using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
//using jaam_butters.Other;




namespace jaam_butters.Agents
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Jaam : DrawableGameComponent
    {
        public static Texture2D SpriteSheet { get; private set; }
        AnimatedSprite sprite;
        SpriteBatch spriteBatch;
        public Vector2 Position { get; set; }
        const float scale = 120f;
        public Controller myController;
        public Rectangle destinationRect
        {
            get
            {
                return new Rectangle((int)Position.X, (int)Position.Y, sprite.Texture.Width, sprite.Texture.Height);
            }
        }
        Vector2 InitPos; 
        Game game;

        bool running=false;
        int currentCommand;
        List<Vector2> commands = new List<Vector2>();
        Vector2 destination;

        public Jaam(Game g, Vector2 position,SpriteBatch sb)
            : base(g)
        {
            destination = InitPos = Position = position;
            spriteBatch = sb;
            game = g;
            LoadContent();
        }

        protected override void LoadContent()
        {
            //if (SpriteSheet == null) SpriteSheet = Game.Content.Load<Texture2D>("C:/Users/Mo/Desktop/EECS 495 2D game/jaam-butters/jaam-buttersContent/Agents/Jaam");
            sprite = new AnimatedSprite(Game.Content.Load<Texture2D>("Jaam"), 1, 31, 32);
            base.LoadContent();
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Draw(sprite.Texture, Position, sprite.SourceRect, Color.White, 0f, sprite.Origin, 1.0f, SpriteEffects.None, 0);

            //base.Draw(gameTime);
        }
        public override void Update(GameTime gameTime)
        {
            if (running && (commands.Count>0)) runCommands(gameTime);
            sprite.HandleSpriteMovement(gameTime);
            if (Math.Abs(Position.X - game.Window.ClientBounds.Width / 2) > 300 || Math.Abs(Position.Y - game.Window.ClientBounds.Height / 2) > 300) ResetCommandReader();
            //base.Update(gameTime);
        }

        public void CommandReader(List<Vector2> commandList,Controller controller)
        {
            myController = controller;
            if (commandList.Count > 0)
            {
                commands = commandList;
                running = true;
                currentCommand = 0;
                destination = Position + commandList[currentCommand]*scale;
            }
        }

        public void ResetCommandReader()
        {
            running = false;
            currentCommand = 0;
            destination = Position = InitPos;
            if(myController!=null)myController.running = false;
        }
        public void runCommands(GameTime gameTime)
        {
            if ((Position - destination).Length() > 0.1)
            {
                Vector2 direction = destination - Position;
                direction.Normalize();
                Position += direction*3.0f;
                
            }
            else
            {
                currentCommand++;
                if (currentCommand < commands.Count)
                {
                    destination += commands[currentCommand] * scale;
                }
                
            }
            figureDirections(gameTime);
        }
        public void figureDirections(GameTime gameTime)
        {
            if (currentCommand < commands.Count)
            {
                if (commands[currentCommand].X > 0) sprite.AnimateRight(gameTime);
                if (commands[currentCommand].X < 0) sprite.AnimateLeft(gameTime);
                if (commands[currentCommand].Y > 0) sprite.AnimateDown(gameTime);
                if (commands[currentCommand].Y < 0) sprite.AnimateUp(gameTime);
            }
        }
    }
}
